package com.xzq.springframework.beans.factory.support;

import com.xzq.springframework.beans.BeansException;
import com.xzq.springframework.beans.factory.BeanFactory;
import com.xzq.springframework.beans.factory.config.BeanDefinition;
import com.xzq.springframework.beans.factory.config.DefaultSingletonBeanRegistry;

/**
 * @Author xzq
 * @Description //  抽象类定义模板方法
 * @Date 2021/11/24 11:34
 * @Version 1.0.0
 **/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        return null;
    }

    @Override
    public <T> T getBean(String beanName, Class<T> requireType) throws BeansException {
        return null;
    }

    /**
     * 模板方法定义 getBean的工作流程
     *      1，从缓存中获取
     *      2，获取Bean实例化信息
     *      3，创建Bean实例
     *
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSinleton(beanName);
        if (bean != null) {
            return bean;
        }
       BeanDefinition beanDefinition=getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);
    }

    public <T> T doGetBean(String beanName, Class<T> requireType, Object... args) {
        return null;
    }

    protected abstract Object createBean(String name, BeanDefinition beanDefinition);

    protected abstract BeanDefinition getBeanDefinition(String name);
}
